﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Core;
using OA.Core.Helper;
using OA.Model;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using OA.Model.DB;
using Z.EntityFramework.Plus;
using OA.Core.EF;
using OA.Model.Mapper;
using OA.Core.DataAccessHelper;
using X.PagedList;

namespace OA.Web.Areas.Member.Controllers
{
    public class MailboxController : BaseMemberController
    {
        private IMemoryCache _cache;
        private ICookie _cookie;
        public MailboxController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }

        public IActionResult Index(int id,string type)
        {
            ViewBag.Type = type.ToLower();
            Mailbox infoMailbox = this.dbContextFactory.DbContext.Mailbox.Where(m => m.ID == id).FirstOrDefault();
            if (infoMailbox == null)
            {
                return Redirect("/Member/Tips/Index?msg=没有找到该抄送");
            }

            if (infoMailbox.FromEmpID!=Info.EmpID && infoMailbox.ToEmpID!=Info.EmpID)
            {
                return Redirect("/Member/Tips/Index?msg=没有找到该抄送");
            }
            
            else if(type.ToLower() != "inbox" && type.ToLower() != "outbox")
            {
                return Redirect("/Member/Tips/Index?msg=没有找到该抄送");
            }

            Voucher infoVoucher = CVoucher.GetVoucherByID(infoMailbox.CustomFormID, infoMailbox.FormID);
            
            this.dbContextFactory.DbContext.Mailbox.Where(m => m.ID == id && m.ToEmpID == Info.EmpID).Update(m => new Mailbox() { Status = 1 });

            CustomForm infoCustomForm = this.dbContextFactory.GetInfo<CustomForm>(infoMailbox.CustomFormID);

            if (infoCustomForm == null)
            {
                return Redirect("/Member/Tips/Index?msg=没有找到该抄送");
            }

            FormGroup infoFormGroup = this.dbContextFactory.DbContext.FormGroup.Where(m => m.ID == infoCustomForm.FormGroupID).FirstOrDefault();
            if (infoFormGroup == null)
            {
                return Redirect("/Member/Tips/Index?msg=没有找到该抄送");
            }
            ViewBag.FormGroup = infoFormGroup;
            if(type.ToLower()=="inbox")
            {
                ViewBag.PageKey = "001006";
            }
            else
            {
                ViewBag.PageKey = "001007";
            }

            ViewBag.FormGroup = infoFormGroup;
            List<ExtendField> extendFields = null;
            if (!string.IsNullOrWhiteSpace(infoVoucher.CustomFormContent))
            {
                extendFields = JsonHelper.DeserializeList<ExtendField>(infoVoucher.CustomFormContent);
            }

            if (extendFields == null)
            {
                extendFields = new List<ExtendField>();
            }
            CVoucher objVoucher = new CVoucher();
            objVoucher.Map(infoVoucher, infoCustomForm, _cache);


            Dictionary<string, string> dicValue = JsonHelper.DeserializeObj<Dictionary<string, string>>(infoVoucher.Extension);


            ViewBag.CustomForm = infoCustomForm;
            ViewBag.ExtendFields = extendFields;//表单结构

            var tableList = this.dbContextFactory.DbContext.TableInfo.ToList();
            return View("~/Areas/Member/Views/CustomForm/Show.cshtml", infoVoucher);
        }

        /// <summary>
        /// 收件箱
        /// </summary>
        /// <returns></returns>
        [MyActionFilterAttribute("001006", "收件箱", "个人中心", true, "iconshoujianxiang2")]
        public IActionResult Inbox()
        {
            return View();
        }

        [MyActionFilterAttribute("001006", "收件箱")]
        public IActionResult GetInboxData(string keyWord, int page = 1, int limit = 20)
        {
            Expression<Func<Mailbox, bool>> where = m => m.ToEmpID == Info.EmpID;
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                where = where.AndAlso<Mailbox>(m => m.Title.Contains(keyWord));
            }          
            var list = this.dbContextFactory.DbContext.Mailbox.Where(where).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }

       

        /// <summary>
        /// 发件箱
        /// </summary>
        /// <returns></returns>
        [MyActionFilterAttribute("001007", "发件箱", "个人中心", true, "iconfajianxiang")]
        public IActionResult Outbox(string keyWord, int p = 1, int size = 20)
        {
            ViewBag.KeyWord = keyWord;
            Expression<Func<Mailbox, bool>> where = m => m.FromEmpID == Info.EmpID;
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                where = where.AndAlso<Mailbox>(m => m.Title.Contains(keyWord));
            }
            PageList<Mailbox> list = this.GetPageList<Mailbox>(where, p, 20);
            return View(list);
        }

        [MyActionFilterAttribute("001007", "发件箱", "个人中心", true, "iconfajianxiang")]
        public IActionResult GetOutboxData(string keyWord, int page = 1, int limit = 20)
        {
            Expression<Func<Mailbox, bool>> where = m => m.FromEmpID == Info.EmpID;
            if (!string.IsNullOrWhiteSpace(keyWord))
            {
                where = where.AndAlso<Mailbox>(m => m.Title.Contains(keyWord));
            }
            var list = this.dbContextFactory.DbContext.Mailbox.Where(where).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }



        [HttpGet]
        public IActionResult Delete(int id)
        {
            BaseResult result = new BaseResult();
            Mailbox infoMailbox=this.dbContextFactory.DbContext.Mailbox.Where(m => m.ID == id && m.FromEmpID == Info.EmpID).FirstOrDefault();
            if (infoMailbox == null)
            {
                result.State = 400;
                result.Msg = "没有找到该抄送信息";
            }
            else if (infoMailbox.Status == 1)
            {
                result.State = 400;
                result.Msg = "已读的抄送,禁止被删除";
            }
            else
            {
                this.Delete<Mailbox>(id);
            }
            return Json(result);
        }


    }
}